﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Duckbill
{
    public class GameState : BaseState
    {
        public Room currRoom;
        public Player player;
        public Dictionary<ObjectType, List<GameObject>> objectList;

        public GameState(Room room)
        {
            currRoom = room;

            player = Game.player;
            //player.pos = new Vector2(192, 256);
            player.room = currRoom;
            
            objectList = new Dictionary<ObjectType, List<GameObject>>();
            Array types = Enum.GetValues(typeof(ObjectType));
            foreach (ObjectType t in types)
                objectList[t] = new List<GameObject>();

            List<GameObject> tempList = new List<GameObject>();

            tempList.AddRange(currRoom.GetObjectsList());
            tempList.Add(player);
            //for (int i = 0; i < 4; i++)
            //{
            //    Enemy e = new Enemy("enemy", 10, new Vector2(500+170*i, 600));
            //    e.room = currRoom;
            //    tempList.Add(e);
            //}
            foreach (GameObject obj in tempList)
                if (!obj.toRemove)
                    objectList[obj.type].Add(obj);

        }

        public override void Update(float time)
        {
            Array types = Enum.GetValues(typeof(ObjectType));
            Dictionary<ObjectType, List<GameObject>> removeList = new Dictionary<ObjectType, List<GameObject>>();
            foreach (ObjectType t in types)
            {
                removeList[t] = new List<GameObject>();
                foreach (GameObject obj in objectList[t])
                {
                    obj.Update(time, objectList);
                    if (obj.toRemove)
                        removeList[t].Add(obj);
                }
            }
            foreach (ObjectType t in types)
                foreach (GameObject obj in removeList[t])
                    objectList[t].Remove(obj);
            foreach (ObjectType t in types)
                foreach (GameObject obj in objectList[t])
                    obj.Move(time);
        }
        public override void Draw(SpriteBatch batch)
        {
         //   currRoom.Draw(batch);
            Array types = Enum.GetValues(typeof(ObjectType));
            foreach (ObjectType t in types)
                foreach (GameObject obj in objectList[t])
                    obj.Draw(batch);
            string toRender = "Jump Level : "+player.JumpLevel.ToString()+
                "\nGun Level : "+player.gunLevel.ToString()
                +"\nJump Dist : "+player.MaxJumpDist.ToString();
            batch.DrawString(Game.font, toRender, new Vector2(32*Sprite.scale.X, 32*Sprite.scale.Y),
                Sprite.drawColor, 0f, Vector2.Zero, Sprite.scale, SpriteEffects.None, 0f);
        }
    }
}
